package com.kunzai.threadrun;

/**
 * java多线程实现的方式，
 */
public class ThredRuntime {
    public static void main(String[] args) {


    }

    /**
     * 守护线程
     */
    private static void methodGuard() {
        //判断是否为后台线程
        System.out.println(
                "main线程是后台线程吗？"
                +Thread.currentThread().isDaemon()
        );
        DamonThread damonThrea = new DamonThread();
        Thread thread = new Thread(damonThrea,"后台线程");
        System.out.println("thread线程默认是后台线程吗？" + thread.isDaemon());
        //将手动创建的线程设置为后台线程
        thread.setDaemon(true);
        thread.start();
        //模拟主线程main的执行任务
        for (int i = 0; i < 6; i++) {
            System.out.println(
                    i
            );
        }
    }

    /**
     * '执行结果是四个线程都在执行这个公共变量一百
     */
    private static void methodTrainOne() {
        RailwayticketTwO railwayticketTwO  = new RailwayticketTwO();
        Thread thread1 = new Thread(railwayticketTwO);
        Thread thread2 = new Thread(railwayticketTwO);
        Thread thread3 = new Thread(railwayticketTwO);
        Thread thread4 = new Thread(railwayticketTwO);
        thread1.setName("窗口壹");
        thread2.setName("窗口贰");
        thread3.setName("窗口弎");
        thread4.setName("窗口肆");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

        /**
         * 这样做的意义是什么？
         * 这样做的意义在于：
         *  你使用继承Thread类重写Run方法这样的方式是：
         *  就要是你四个对象都创建了Run方法那么就存在了四个方法
         *  所以你执行的话就没有达到那个变量共享的问题。
         *
         *  RailwayticketTwO：只是创建了这一个类，那么就只是创建了一个Run方法
         *
         */
    }

    /**
     * 卖票实验第一次
     */
    private static void method1TRain() {
        RailwayticketOne railwayticket1 = new RailwayticketOne();
        railwayticket1.setName("窗口壹");
        RailwayticketOne railwayticket2 = new RailwayticketOne();
        railwayticket2.setName("窗口贰");
        RailwayticketOne railwayticket3 = new RailwayticketOne();
        railwayticket3.setName("窗口弎");
        RailwayticketOne railwayticket4 = new RailwayticketOne();
        railwayticket4.setName("窗口肆");

        //开启线程
        railwayticket1.start();
        railwayticket2.start();
        railwayticket3.start();
        railwayticket4.start();

        /**
         * 执行结果是每个窗口并没有去售卖这个一百张
         * 而是每个窗口单独去卖一百张票那么也就是说
         * 有四百张票，其实是总账只有一百达不到目的
         */}
}
